package com.ailanyin.modules.system.controller;

import com.ailanyin.common.base.domain.BaseQuery;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.common.log.annotation.Log;
import com.ailanyin.common.log.enums.BusinessType;
import com.ailanyin.common.poi.utils.ExcelUtil;
import com.ailanyin.modules.system.domain.SysConfig;
import com.ailanyin.modules.system.service.ISysConfigService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/config")
public class SysConfigController {
    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("hasAuthority('system:config:list')")
    @GetMapping("/list")
    public CommonPage list(SysConfig config,
                           @RequestParam(value = "pageSize" , defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysConfig> list = configService.selectConfigList(config);
        return CommonPage.restPage(list);
    }

    @Log(title = "参数管理" , businessType = BusinessType.EXPORT)
    @PreAuthorize("hasAuthority('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfig config) throws Exception {
        List<SysConfig> list = configService.selectConfigList(config);
        ExcelUtil.exportExcel(list, SysConfig.class, "参数数据" , response);
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("hasAuthority('system:config:query')")
    @GetMapping
    public AjaxResult getInfo(@RequestParam("configId") Long configId) {
        return AjaxResult.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey) {
        return AjaxResult.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("hasAuthority('system:config:add')")
    @Log(title = "参数管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysConfig config) {
        return AjaxResult.toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("hasAuthority('system:config:edit')")
    @Log(title = "参数管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysConfig config) {
        return AjaxResult.toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("hasAuthority('system:config:remove')")
    @Log(title = "参数管理" , businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody BaseQuery<Long> query) {
        configService.deleteConfigByIds(query.getIds());
        return AjaxResult.success();
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("hasAuthority('system:config:remove')")
    @Log(title = "参数管理" , businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache() {
        configService.resetConfigCache();
        return AjaxResult.success();
    }
}
